import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Main from '../components/Main.vue'
import User from '../components/system/User.vue'
import Menu from '../components/system/Menu.vue'
import Reserve from '../components/ey/Reserve.vue'
import Schedule from '@/components/ey/Schedule.vue'
Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      name: 'Login',
      component: Login
    },
    {
      path: '/home',
      name: 'Home',
      component: Home,
      redirect: '/main',
      children: [
        {
          path: '/main',
          name: 'main',
          component: Main
        },{
          path: '/system/user/list',
          name: 'user',
          component: User
        },{
          path: '/system/menu/list',
          name:'menu',
          component: Menu
        },{
          path: '/ey/reserve',
          name:'reserve',
          component: Reserve
        },{
         path: '/ey/schedule',
          name:'schedule',
          component: Schedule
        }
      ]
    }]
})


router.beforeEach((to, from, next) => {
  const token = sessionStorage.getItem('token');

  if (to.name === 'Login') {
    if (token) {
      next('/home');
    } else {
      next();
    }
  } else {
    if (!token) {
      next('/login');
    } else {
      next();
    }
  }
});



export default router
